package com.kaifamiao.creation;

import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Fourth {

    public static void main(String[] args) throws Exception {

        // 创建线程池
        ExecutorService es = Executors.newFixedThreadPool(3);
        System.out.println( es );

        Runnable runnable = () -> {
            System.out.println( "runnable" );
            System.out.println( Thread.currentThread().getName() );
            System.out.println( es );
        };
        // 向线程池提交任务
        es.submit( runnable );

        Callable<String> callable = () -> {
            System.out.println( "callable" );
            System.out.println( Thread.currentThread().getName() );
            System.out.println( es );
            return UUID.randomUUID().toString();
        };
        // 向线程池提交任务
        Future<String> future = es.submit( callable );

        // 向线程池提交任务
        es.submit( () -> {
            while(true) {
                if( future.isDone() ) {
                    System.out.println( future.get() );
                    break;
                }
            }
            return null;
        } );

        // 关闭线程池
        es.shutdown();
    }

}
